//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenLS.Chart.Internals;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    ///<summary>
    /// Represents an area series is an area chart.
    ///</summary>
    public class AreaSeries : Series, INumericValuesProvider, IDataPointOwner, IDataLabelsOwner
    {
        private NumericDataSource _values;
        private DataSource _cat;
        private DataLabels _dlbls;
        private readonly DataPoints _dPt = new DataPoints();
        private ErrorBars _errorBars;
        private PictureOptions _pictureOptions;
        private readonly ObservableCollection<TrendLine> _trendLines = new ObservableCollection<TrendLine>();

        public ObservableCollection<TrendLine> TrendLines
        {
            get { return _trendLines; }
        }

        public DataSource Categories
        {
            get { return _cat; }
            set { _cat = value; notifyPropertyChanged("Categories");}
        }

        public NumericDataSource Values
        {
            get { return _values; }
            set
            {
                if (_values != null)
                    _values.ValueChanged -= numericDataSource_ValueChanged;
                _values = value;
                notifyPropertyChanged("Values");
                if (_values != null)
                    _values.ValueChanged += numericDataSource_ValueChanged;
            }
        }

        public DataPoints DataPoints
        {
            get { return _dPt; }
        }

        public ErrorBars ErrorBars
        {
            get { return _errorBars; }
            set { _errorBars = value;notifyPropertyChanged("ErrorBars"); }
        }


        public DataLabels DataLabels
        {
            get { return _dlbls; }
            set { _dlbls = value; notifyPropertyChanged("DataLabels");}
        }

        public PictureOptions PictureOptions
        {
            get { return _pictureOptions; }
            set { _pictureOptions = value; notifyPropertyChanged("PictureOptions");}
        }

        #region INumericValuesProvider Members

        public IEnumerable<NumericValue> GetValues()
        {
            return Values.NumericValues;
        }


        public event EventHandler ValueChanged;

        #endregion

        private void numericDataSource_ValueChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null)
                ValueChanged(sender, e);
        }


        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("ser"))
            {
                ReadXmlBase(c);
                PictureOptions = c.ReadOptionalElement<PictureOptions>("pictureOptions");
                DataPoints.Clear();
                while (context.ReaderLocalName == "dPt")
                {
                    DataPoints.Add(context.ReadElement<DataPoint>());
                }
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                while (c.ReaderLocalName == TrendLine.xmlName)
                {
                    TrendLines.Add(c.ReadElement<TrendLine>());
                }
                ErrorBars = c.ReadOptionalElement<ErrorBars>(ErrorBars.xmlName);
                _cat = DataSource.ReadOptionalDataSource("cat", c);
                Values = NumericDataSource.ReadNumericDataSource("val", c);

                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("ser"))
            {
                WriteXmlBase(c);
                c.WriteOptionalElement(PictureOptions);
                foreach (DataPoint _dPt in DataPoints)
                    c.WriteElement(_dPt);
                c.WriteOptionalElement(DataLabels);
                foreach (TrendLine l in TrendLines)
                    c.WriteElement(l);
                c.WriteOptionalElement(ErrorBars);
                WriteOptionalNumericDataSource("cat", Categories, c);
                WriteOptionalNumericDataSource("val", Values, c);
                c.WriteOptionalElement(Extension);
            }
        }
    }
}